<nz-card>
  <nz-descriptions
    nzTitle="基本信息"
    nzBordered
    nzSize="small"
    [nzColumn]="{ xxl: 4, xl: 3, lg: 3, md: 3, sm: 2, xs: 1 }"
  >
    <nz-descriptions-item nzTitle="单号">
      {{ detailsData.purchaseNumber || '-' }}
      <i
        *ngIf="detailsData.purchaseNumber"
        nz-icon
        nzType="copy"
        nzTheme="outline"
        nz-tooltip
        nzTooltipTitle="点击复制"
        (click)="coprText(detailsData.purchaseNumber)"
      ></i>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="状态">
      <span [ngStyle]="{'color': detailsData.state | storagePurchaseState: 'color'}">
        {{ detailsData.state | storagePurchaseState: 'label' }}
      </span>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="新建时间">{{ detailsData.createTime || '-' }}</nz-descriptions-item>
    <nz-descriptions-item nzTitle="新建人">{{ detailsData.applicant || '-' }}</nz-descriptions-item>
    
    <nz-descriptions-item nzTitle="供应商">
      {{ filterText(supplierOptions, detailsData.supId, 'id') }}
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="预定交货日期">{{ detailsData.deliveryTime || '-' }}</nz-descriptions-item>
    <nz-descriptions-item nzTitle="结案时间" nzSpan="2">{{ detailsData.closingTime || '-' }}</nz-descriptions-item>

    <nz-descriptions-item nzTitle="附件" nzSpan="4">
      <ul>
        <li *ngFor="let item of detailsData?.fileList">
          {{ item.name }}
          <button
            nz-button
            nzType="link"
            nzSize="small"
            (click)="downFile(item)"
          >下载</button>
        </li>
      </ul>
    </nz-descriptions-item>
    <nz-descriptions-item nzTitle="备注" nzSpan="4">{{ detailsData.remark || '-' }}</nz-descriptions-item>
  </nz-descriptions>
</nz-card>

<nz-card class="m-t-4">
  <p class="detail-title">采购明细</p>

  <div class="table-wrap">
    <nz-table
      #mxTable
      nzBordered
      nzSize="small"
      [nzFrontPagination]="false"
      [nzShowPagination]="false"
      [nzData]="detailedTableData">
      <thead>
        <tr>
          <th nzAlign="center" rowspan="2" nzWidth="70px">序号</th>
          <th nzAlign="center" colspan="5">商品信息</th>
          <th nzAlign="center" colspan="6">采购信息</th>
          <th nzAlign="center" rowspan="2">操作</th>
        </tr>
        <tr>
          <th nzAlign="center">商品状态</th>
          <th nzAlign="center">商品名称</th>
          <th nzAlign="center">商品编号</th>
          <th nzAlign="center">商品69码</th>
          <th nzAlign="center">标准采购价(含税)</th>

          <th nzAlign="center">行状态</th>
          <th nzAlign="center">采购数量</th>
          <th nzAlign="center">冻结数量</th>
          <th nzAlign="center">已入库数量</th>
          <th nzAlign="center">采购单价(含税)</th>
          <th nzAlign="center">小计</th>
        </tr>
      </thead>
      <tbody>
        <tr
          *ngFor="let data of mxTable.data; let index = index;"
          [ngClass]="{'tr-bg-red': data?.lineState === 1}">
          <!-- 序号 -->
          <td nzAlign="center">{{ index + 1 }}</td>
          <!-- 商品状态 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : '#67C23A'}">
              {{ data.whGoodsState === 0 ? '禁用' : (data.whGoodsState === 1 ? '启用' : '-') }}
            </span>
          </td>
          <!-- 商品名称 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsName || '-' }}
            </span>
          </td>
          <!-- 商品编号 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsCode || '-' }}
            </span>
          </td>
          <!-- 商品69码 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.whGoodsState === 0 ? '#999999' : ''}">
              {{ data?.whGoodsTwoCode || '-' }}
            </span>
          </td>
          <!-- 标准采购价(含税) -->
          <td nzAlign="center">
            {{ data.standardCostPrice || '-' }}
          </td>
          
          <!-- 行状态 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data?.lineState === 0 ? '#67C23A' : '#F56C6C'}">
              {{ data?.lineState === 0 ? '正常' : (data?.lineState === 1 ? '已作废' : '-') }}
            </span>
          </td>
          <!-- 采购数量 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.purchaseNumber !== data.stockNumber ? '#FF9900' : ''}">
              {{ data.purchaseNumber || '0' }}
            </span>
          </td>
          <!-- 冻结数量 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.purchaseNumber !== data.stockNumber ? '#FF9900' : ''}">
              {{ data.frozenNumber || '-' }}
            </span>
          </td>
          <!-- 已入库数量 -->
          <td nzAlign="center">
            <span [ngStyle]="{'color': data.purchaseNumber !== data.stockNumber ? '#FF9900' : ''}">
              {{ data.stockNumber || '0' }}
            </span>
          </td>
          <!-- 购单价(含税) -->
          <td nzAlign="right">
            {{ data.purchasePrice | number: '1.4-4' }}
          </td>
          <!-- 小计 -->
          <td nzAlign="right">
            {{ calcProduct(data.purchaseNumber, data.purchasePrice) | number: '1.4-4' }}
          </td>
          <!-- 操作 -->
          <td nzAlign="center">
            <ng-container *ngIf="data?.lineState === 0; else elseTemplate">
              <button
                *ngIf="permission.userPermission.has('storageCenter:purchase:toVoid') && detailsData.state === 2 && data.lineState === 0"
                nz-button
                nzType="link"
                nzSize="small"
                nzDanger
                (click)="showTovoidModal(data)"
              >作废</button>
            </ng-container>
            <ng-template #elseTemplate>
              -
            </ng-template>
          </td>
        </tr>

        <tr *ngIf="detailedTableData?.length" style="background: #fafafa; font-weight: 600;">
          <td nzAlign="center" colSpan="7">合计</td>
          <!-- 采购数 -->
          <td nzAlign="center">{{ calcSumTotal('purchaseNumber') }}</td>
          <!-- 冻结数 -->
          <td nzAlign="center">{{ calcSumTotal('frozenNumber') }}</td>
          <!-- 已入库数量 -->
          <td nzAlign="center">{{ calcSumTotal('stockNumber') }}</td>
          <td nzAlign="center">-</td>
          <td nzAlign="right">{{ calcProductTotal() | number: '1.4-4' }}</td>
          <td nzAlign="center">-</td>
        </tr>
      </tbody>
    </nz-table>
  </div>
</nz-card>

<nz-card class="m-t-4">
  <div nz-row nzJustify="center">
    <button
      nz-button
      nzType="default"
      (click)="pageBack()"
    >返回</button>
  </div>
</nz-card>

<!-- S 作废 Modal -->
<nz-modal
  nzClassName="custom-model-wrap"
  [nzFooter]="null"
  [(nzVisible)]="isZfVisible"
  [nzTitle]="'提示'"
  (nzOnCancel)="isZfVisible = false">

  <div *nzModalContent>
    <div class="custom-model-body">
      <p class="zf-tips">该操作不可逆，作废后该行将无法入库，也不会被统计到</p>
      
      <nz-form-item>
        <nz-form-label nzRequired>备注</nz-form-label>
        <nz-form-control>
          <nz-textarea-count [nzMaxCharacterCount]="100">
            <textarea
              rows="4"
              nz-input
              maxlength="100"
              placeholder="请输入备注、必填"
              [(ngModel)]="toVoidRemark"
            ></textarea>
          </nz-textarea-count>
        </nz-form-control>
      </nz-form-item>
    </div>

    <div class="custom-model-foot">
      <div class="custom-foot-cont">
        <button nz-button nzType="default" (click)="isZfVisible = false">取消</button>
        <button nz-button nzType="primary" [nzLoading]="submitZfLoading" (click)="submitToVoidData()">确定作废</button>
      </div>
    </div>
  </div>
</nz-modal>
<!-- S 作废 Modal -->